import BaseView from "../../../cfw/mvc/BaseView";
import DininghallController from "../DininghallController";
import FoodItemModel from "../model/FoodItemModel";
import DininghallManager from "../model/DininghallManager";
import BagManager from "../../bag/BagManager";
import ModuleManager, { ModuleID } from "../../../cfw/module/ModuleManager";
import { ResType } from "../../../cfw/res/ResInterface";
import ResItem from "../../../cfw/res/ResItem";
import LayerMgr from "../../../engine/LayerMgr";
import DragItemView from "../../item/DragItemView";
import { GameText, EventName, ActionType } from "../../../public/Config";
import TipController from "../../tip/TipController";
import GlobalEvent from "../../../cfw/event/GlobalEvent";
import { ItemState } from "../../../cfw/tools/Define";
import SDKManager from "../../../sdk/SDKManager";
import { SDKEventName } from "../../../sdk/SDKConfig";


const { ccclass, property } = cc._decorator;

@ccclass
export default class DininghallView extends BaseView {


    @property([cc.Node])
    posList: cc.Node[] = []
    @property(cc.Node)
    playerRect: cc.Node = null;

    @property(cc.Sprite)
    item: cc.Sprite = null;

    protected controller: DininghallController;
    protected clickIndex: number = -1;
    protected initPos: cc.Vec3 = cc.v3()
    protected model: DininghallManager;
    protected clickIndexList: number[] = []

    protected items: DragItemView[] = []

    delegate(layerMgr: LayerMgr, index: number, item: cc.Node) {
        // cc.log(' delegate index ', index)
        let list: FoodItemModel[] = this.model.getList()
        let model = list[index]
        let comp = this.controller.initView(item, 'DragItemView', model) as DragItemView
        comp.updatePosition(this.posList[index].getPosition())
        // if (comp.isLoad()) {
        this.items[index] = comp;
        comp.refresh()
        // }
    }

    onDestroy() {
        GlobalEvent.instance().off(EventName.EATING_FINISH, this.eatingFinish, this)
    }

    start() {
        this.node.on(cc.Node.EventType.TOUCH_START, this.touchStart, this)
        this.node.on(cc.Node.EventType.TOUCH_MOVE, this.touchMove, this)
        this.node.on(cc.Node.EventType.TOUCH_END, this.touchEnd, this)
        this.node.on(cc.Node.EventType.TOUCH_CANCEL, this.touchEnd, this)
        GlobalEvent.instance().on(EventName.EATING_FINISH, this.eatingFinish, this)
        // let list: FoodItemModel[] = this.model.getList()
        // for (let index = 0; index < this.items.length; index++) {
        //     const element = this.items[index];
        //     if (list[index]) {
        //         element.setModel(list[index])
        //     }
        // }
    }
    touchStart(e: cc.Touch) {
        if (this.item.spriteFrame) {
            return;
        }
        if (this.clickIndex == -1) {
            let pos = e.getLocation();
            for (let index = 0; index < this.items.length; index++) {
                const element = this.items[index];
                if (element.getBoundingBoxToWorld().contains(pos)) {
                    if (!element.isOpen()) {
                        TipController.instance().showToast(GameText.ITEM_TOAST, { num: element.getOpenLevel() })
                        return;

                    }
                    if (element.needBuy()) {
                        this.controller.openItem(element.getModel())

                    } else {
                        this.clickIndex = index;
                        this.initPos.x = this.items[this.clickIndex].node.x;
                        this.initPos.y = this.items[this.clickIndex].node.y;
                        cc.log(' 点击到 是 index ', this.clickIndex)

                    }

                    break;
                }
            }
        }
    }

    touchEnd() {
        if (this.clickIndex >= 0) {

            this.collidePlayer();
            this.clickIndex = -1;
        }

    }
    touchMove(e: cc.Touch) {
        if (this.clickIndex >= 0) {
            let pos = e.getLocation();
            pos = this.node.convertToNodeSpaceAR(pos)
            this.items[this.clickIndex].node.x = pos.x;
            this.items[this.clickIndex].node.y = pos.y;
        }

    }

    onButtonHomeClick() {
        this.controller.back()
    }

    onButtonNextClick() {
        this.model.updateExp()
        this.controller.gotoNext()
    }

    eatingFinish() {
        this.item.spriteFrame = null;
    }

    collidePlayer() {
        let rect = this.playerRect.getBoundingBox()
        let pos = this.items[this.clickIndex].node.getPosition()
        // cc.log('this.playerRect.getBoundingBoxToWorld() ', rect.x, rect.y, rect.width, rect.height, ' pos ', pos.x, pos.y)
        if (rect.contains(pos)) {
            // cc.log(" 松开的是哪一个 ", this.clickIndex)
            let item = this.items[this.clickIndex];
            item.setState(ItemState.NOT_GET)
            // item.node.x = this.playerRect.x;
            // item.node.y = this.playerRect.y;
            ModuleManager.loadRes(item.getIcon(), ResType.SpriteFrame, (err, item: ResItem) => {
                if (err || !cc.isValid(this.node)) {
                    return;
                }
                this.item.spriteFrame = item.getRes()
            })
            GlobalEvent.instance().emit(EventName.CHANGE_PLAYER_ACTION, ActionType.EATING)
            this.model.addNum(item.getModel())
            SDKManager.getChannel().sendEvent(SDKEventName.eating, item.getID())
            if (!this.clickIndexList[this.clickIndex]) {
                this.clickIndexList[this.clickIndex] = 1;
                BagManager.addCoin(10, item.node.parent.convertToWorldSpaceAR(item.getPosition()))
            }
            cc.tween(this).to(1, {}).call(() => {

                this.collideLogic()
            }).start()
            this.items[this.clickIndex].node.x = this.initPos.x;
            this.items[this.clickIndex].node.y = this.initPos.y;
        } else {
            this.items[this.clickIndex].node.x = this.initPos.x;
            this.items[this.clickIndex].node.y = this.initPos.y;
        }
    }

    collideLogic() {

    }

}
